package com.chenjiajin.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 性别converter转换器
 *
 * @author fraser
 * @date 2019/10/7 9:26 AM
 */
public class GenderConverter implements Converter<Integer> {

    public static final String MALE = "男";
    public static final String FEMALE = "女";

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 导入的时候 将中文转为对应的数值
     */
    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        String stringValue = cellData.getStringValue();
        if (MALE.equals(stringValue)) {
            return 1;
        } else {
            return 2;
        }
    }


    /**
     * 导出的时候 将数值转为对应的中文
     */
    @Override
    public CellData<Object> convertToExcelData(Integer value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        CellData<Object> cellData = new CellData<>("男");
        if (value.equals(1)) {
            cellData.setStringValue("男");
        }
        if (value.equals(2)) {
            cellData.setStringValue("女");
        }
        return cellData;

        //return new CellData<>(value.equals(1) ? "男" : "女");

    }


}

